﻿using System;
using System.Collections;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using DAL;
using CommonComponent.Common;

/// <summary>
/// 构建TreeView树
/// </summary>
public class TreeViewManager
{
    public TreeViewManager()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    #region 创建导航菜单树
    /// <summary>
    /// 功能：创建导航菜单树
    /// 适用场合：自链表（用当前节点，父结点），用URL字段
    /// </summary>
    /// <param name="TreeView1">TreeView控件</param>
    /// <param name="treeParams">参数类</param>
    /// <returns>TreeView</returns>
    public TreeView CreateMenuTree(TreeView TreeView1, TreeParams treeParams)
    {
        SqlConnection oConn = new SqlConnection(DbHelperSql.connectionString);
        SqlDataReader oDr;
        Hashtable htTreeNode = new Hashtable();

        try
        {
            oConn.Open();
            SqlCommand oCmd = new SqlCommand(treeParams.SQLSelect, oConn);
            oDr = oCmd.ExecuteReader();
            while (oDr.Read())
            {
                TreeNode myNodes = new TreeNode();
                string iMyNodeId = (string)oDr[treeParams.FieldCurrNodeID];
                htTreeNode.Add(iMyNodeId, myNodes);
            }
            oDr.Close();
            
            oDr = oCmd.ExecuteReader();

            //从表中取数据
            while (oDr.Read())
            {
                string iParentNodeId = (string)oDr[treeParams.FieldParentID];
                string iMyNodeId = (string)oDr[treeParams.FieldCurrNodeID];
                string nodeText = (string)oDr[treeParams.FieldText];

                if (iParentNodeId != treeParams.HeadMark)
                {
                    TreeNode myNodes = htTreeNode[iMyNodeId] as TreeNode;
                    myNodes.Text = nodeText;
                    myNodes.Value = iMyNodeId;
                    if (oDr[treeParams.FieldUrl].ToString() != "")
                    {
                        myNodes.NavigateUrl = oDr[treeParams.FieldUrl].ToString();
                        myNodes.Target = treeParams.Target;
                    }
                    else
                    {
                        myNodes.SelectAction = TreeNodeSelectAction.Expand;
                    }
                    
                    TreeNode ParentNode = htTreeNode[iParentNodeId] as TreeNode;
                    ParentNode.ChildNodes.Add(myNodes);
                }
                else
                {
                    TreeNode myNodes = htTreeNode[iMyNodeId] as TreeNode;
                    myNodes.Text = nodeText;
                    myNodes.Value = iMyNodeId;
                    if (oDr[treeParams.FieldUrl].ToString() != "")
                    {
                        myNodes.NavigateUrl = oDr[treeParams.FieldUrl].ToString();
                        myNodes.Target = treeParams.Target;
                    }
                    else
                    {
                        myNodes.SelectAction = TreeNodeSelectAction.Expand;
                    }
                    myNodes.NavigateUrl = oDr[treeParams.FieldUrl].ToString();
                    TreeView1.Nodes.Add(myNodes);
                }
            }
        }
        catch (System.Exception sqle)
        {
            Logger.error(this.GetType(),sqle.Message,"","");
        }
        finally
        {
            if (oConn != null)
                oConn.Close();
        }
        return TreeView1;
    }

    #endregion

    #region 带用CheckBox树
    /// <summary>
    /// 功能：创建带有CheckBox的非级联选择树
    /// 适用场合：自链表（用当前节点，父结点），适用于多选组件
    /// </summary>
    /// <param name="TreeView1">TreeView控件</param>
    /// <param name="treeParams">参数类</param>
    /// <returns>TreeView</returns>
    public TreeView CreateCheckBoxTree(TreeView TreeView1, TreeParams treeParams)
    {
        SqlConnection oConn = new SqlConnection(DbHelperSql.connectionString);
        SqlDataReader oDr;
        Hashtable htTreeNode = new Hashtable();
        TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
        try
        {
            oConn.Open();
            SqlCommand oCmd = new SqlCommand(treeParams.SQLSelect, oConn);
            oDr = oCmd.ExecuteReader();
            while (oDr.Read())
            {
                TreeNode myNodes = new TreeNode();
                string iMyNodeId = (string)oDr[treeParams.FieldCurrNodeID];
                htTreeNode.Add(iMyNodeId, myNodes);
            }
            oDr.Close();

            oDr = oCmd.ExecuteReader();

            //从表中取数据
            while (oDr.Read())
            {
                string iParentNodeId = (string)oDr[treeParams.FieldParentID];
                string iMyNodeId = (string)oDr[treeParams.FieldCurrNodeID];

                if (iParentNodeId != treeParams.HeadMark)
                {
                    TreeNode myNodes = htTreeNode[iMyNodeId] as TreeNode;
                    myNodes.Text = oDr[treeParams.FieldText].ToString();
                    myNodes.Value = iMyNodeId;

                    myNodes.SelectAction = TreeNodeSelectAction.Expand;

                    TreeNode ParentNode = htTreeNode[iParentNodeId] as TreeNode;
                    ParentNode.ChildNodes.Add(myNodes);
                }
                else
                {
                    TreeNode myNodes = htTreeNode[iMyNodeId] as TreeNode;
                    myNodes.Text = oDr[treeParams.FieldText].ToString();
                    myNodes.Value = iMyNodeId;

                    myNodes.SelectAction = TreeNodeSelectAction.Expand;

                    TreeView1.Nodes.Add(myNodes);
                }
            }
        }
        catch (System.Exception sqle)
        {
            //sqle.ToString().Replace("\n", "<br>");
            Logger.error(this.GetType(),sqle.Message,"","");
        }
        finally
        {
            if (oConn != null)
                oConn.Close();
        }
        return TreeView1;
    }

    #endregion

    #region 通用树
    /// <summary>
    /// 创建通用树
    /// 功能：对每个节点的文字点击都触发JS事件，对节点展开/收缩操作都需要点击图标
    /// 应用：如Windows系统中的资源管理器
    /// </summary>
    /// <param name="TreeView1">TreeView控件</param>
    /// <param name="treeParams">TreeParams参数类</param>
    /// <returns></returns>
    public TreeView CreateCommonTree(TreeView TreeView1, TreeParams treeParams)
    {
        SqlConnection oConn = new SqlConnection(DbHelperSql.connectionString);
        SqlDataReader oDr;
        Hashtable htTreeNode = new Hashtable();
        try
        {
            oConn.Open();
            SqlCommand oCmd = new SqlCommand(treeParams.SQLSelect, oConn);  
            oDr = oCmd.ExecuteReader();

            while (oDr.Read())
            {
                TreeNode myNodes = new TreeNode();
                string iMyNodeId = (string)oDr[treeParams.FieldCurrNodeID];
                htTreeNode.Add(iMyNodeId, myNodes);
            }
            oDr.Close();

            oDr = oCmd.ExecuteReader();
            
            //从表中取数据
            while (oDr.Read())
            {
                string iParentNodeId = (string)oDr[treeParams.FieldParentID];
                string iMyNodeId = (string)oDr[treeParams.FieldCurrNodeID];

                if (iParentNodeId != treeParams.HeadMark)
                {
                    TreeNode myNodes = htTreeNode[iMyNodeId] as TreeNode;
                    myNodes.Text = oDr[treeParams.FieldText].ToString();
                    myNodes.Value = iMyNodeId;
                    myNodes.NavigateUrl = "javascript:" + treeParams.JSFunctionName + @"('" + iMyNodeId + @"','" + myNodes.Text + @"');";
                    TreeNode ParentNode = htTreeNode[iParentNodeId] as TreeNode;
                    ParentNode.ChildNodes.Add(myNodes);
                }
                else
                {
                    TreeNode myNodes = htTreeNode[iMyNodeId] as TreeNode;
                    myNodes.Text = oDr[treeParams.FieldText].ToString();
                    myNodes.Value = iMyNodeId;
                    myNodes.NavigateUrl = "javascript:" + treeParams.JSFunctionName + @"('" + iMyNodeId + @"','" + myNodes.Text + @"');";
                    TreeView1.Nodes.Add(myNodes);
                }
            }
        }
        catch (System.Exception sqle)
        {
            //sqle.ToString().Replace("\n", "<br>");
            //Response.Write(sqle);
        }
        finally
        {
            if (oConn != null)
                oConn.Close();
        }
        return TreeView1;
    }

    /// <summary>
    /// 功能：创建组织机构与人员树
    /// 适用场合：1、机构表是一张自链表；2、人员表是机构表的细表；
    /// </summary>
    /// <param name="TreeView1">TreeOrganAndEmpParams实体类</param>
    /// <param name="treeParams">TreeView对象</param>
    /// <returns></returns>
    public TreeView CreateOrganAndEmpTree(TreeView TreeView1, TreeOrganAndEmpParams treeParams)
    {
        SqlConnection oConn = new SqlConnection(DbHelperSql.connectionString);
        SqlDataReader oDr;
        Hashtable htTreeNode = new Hashtable();
        try
        {
            oConn.Open();
            SqlCommand oCmd = new SqlCommand(treeParams.SQLSelect, oConn);  
            oDr = oCmd.ExecuteReader();
            while (oDr.Read())
            {
                TreeNode myNodes = new TreeNode();
                string iMyNodeId = (string)oDr[treeParams.FieldCurrNodeID];
                htTreeNode.Add(iMyNodeId, myNodes);
            }
            oDr.Close();

            oDr = oCmd.ExecuteReader();

            //从表中取数据
            while (oDr.Read())
            {
                //根节点
                if (oDr[treeParams.FieldParentID].Equals(treeParams.HeadMark))
                {
                    string iNodeId = (string)oDr[treeParams.FieldCurrNodeID];
                    TreeNode myNodes = htTreeNode[iNodeId] as TreeNode;
                    myNodes.Text = oDr[treeParams.FieldText].ToString();
                    myNodes.Value = iNodeId;
                    
                    //maudyi 2009-04-13,变为点击组织机构也选中返回，而不仅仅是展开而已,
                    myNodes.SelectAction = TreeNodeSelectAction.Select;
                    //myNodes.SelectAction = TreeNodeSelectAction.Expand;
                    myNodes.NavigateUrl = "javascript:" + treeParams.JSFunctionName + @"('" + Constants.TREE_NODE_ID_ORG_PREFIX + iNodeId + @"','" + myNodes.Text + @"');";
                    TreeView1.Nodes.Add(myNodes);
                    continue;
                }
                string iParentNodeId = StringUtil.ParseDBNull(oDr[treeParams.FieldParentID]);
                string iMyNodeId = StringUtil.ParseDBNull(oDr[treeParams.FieldCurrNodeID]);

                //树枝节点
                if (iParentNodeId != treeParams.HeadMark)
                {
                    TreeNode myNodes = htTreeNode[iMyNodeId] as TreeNode;
                    myNodes.Text = oDr[treeParams.FieldText].ToString();
                    myNodes.Value = iMyNodeId;
                    //maudyi 2009-04-13,变为点击组织机构也选中返回，而不仅仅是展开而已
                    myNodes.SelectAction = TreeNodeSelectAction.Select;
                    myNodes.NavigateUrl = "javascript:" + treeParams.JSFunctionName + @"('" + Constants.TREE_NODE_ID_ORG_PREFIX + myNodes.Value + @"','" + myNodes.Text + @"');";
                    myNodes.ImageUrl = "../Admin_Images/FOLDER.GIF";
                    TreeNode ParentNode = htTreeNode[iParentNodeId] as TreeNode;
                    ParentNode.ChildNodes.Add(myNodes);
                }
                //还是根节点？
                else
                {
                    TreeNode myNodes = htTreeNode[iMyNodeId] as TreeNode;
                    myNodes.Text = oDr[treeParams.FieldText].ToString();
                    myNodes.Value = iMyNodeId;
                    //maudyi 2009-04-13,变为点击组织机构也选中返回，而不仅仅是展开而已
                    myNodes.SelectAction = TreeNodeSelectAction.Select;
                    TreeView1.Nodes.Add(myNodes);

                }
            }

            oDr.Close();
            SqlCommand empCmd = new SqlCommand(treeParams.SQLChildSelect, oConn);
            SqlDataReader empDr = empCmd.ExecuteReader();

            while (empDr.Read())
            {
                string empOrganID = (string)empDr[treeParams.FieldEmpOrganID];
                if (String.IsNullOrEmpty(empOrganID) == false && htTreeNode.Contains(empOrganID))
                {
                    string empID = (string)empDr[treeParams.FieldEmpID];
                    string empName = (string)empDr[treeParams.FieldEmpName];

                    TreeNode myNodes = new TreeNode();
                    myNodes.Text = empName;
                    myNodes.Value = empID;
                    myNodes.NavigateUrl = "javascript:" + treeParams.JSFunctionName + @"('" + empID + @"','" + empName + @"');";
                    TreeNode ParentNode = htTreeNode[empOrganID] as TreeNode;
                    ParentNode.ChildNodes.Add(myNodes);
                }
            }
        }
        catch (System.Exception sqle)
        {
            //sqle.ToString().Replace("\n", "<br>");
            //Response.Write(sqle);
        }
        finally
        {
            if (oConn != null)
                oConn.Close();
        }
        return TreeView1;
    }




    #endregion

}




